<?php
/**
 * 会员模型
 * @author maniac
 */
namespace app\common\model;

class Members extends SoftDel
{
    public $tableName = '会员';             // 数据表名称
    public $logId     = ['id', 'username']; // 日志操作相关标识
    public $softId    = 1;                  // 软删除标识
    
    protected $autoWriteTimestamp = true;   // 自动写入时间戳
    
    // 自动转换
    protected $type = [
        'checked' => 'integer',
        'status'  => 'integer',
    ];
    public function fields(){
        $field = 'id,parent_id,password,nickname,image_uri,points,balance,commission,money';
        return $field;
    }

    //获取门店id
    public function getBusinessIdAttr($value,$data){
        return Business::where("user_id",$data['id'])->field('id')->find()['id'];
    }
    //获取性别
    public function getSexTxtAttr($value,$data){
        $sex = WechatUsers::where("id",$data['wechat_id'])->value('sex');
        $txt = '';
        if ($sex == 0){
            $txt = "未知";
        }
        if ($sex == 1){
            $txt = "男";
        }
        if ($sex == 2){
            $txt = "女";
        }
        return $txt;
    }

    /**
     * 微信昵称获取器
     * @param string $value 字段值
     * @param array $data  所有数据
     */
    public function getNicknameWxAttr($value, $data)
    {
        $wechat = $this->getWechat($data['wechat_id']);
        return $wechat['nickname'];
    }


    /**
     * 上级昵称获取器
     * @param string $value 字段值
     * @param array $data  所有数据
     */
    public function getParentTxtAttr($value, $data)
    {
        return Members::where('id',$data['parent_id'])->value('nickname');
//        $wechat_id=Members::where('id',$data['parent_id'])->value('nickname');
//        $wechat = $this->getWechat($wechat_id);
//        return $wechat['nickname']?$wechat['nickname']:'无';
    }
    
    /**
     * 微信头像获取器
     * @param string $value 字段值
     * @param array $data  所有数据
     */
    public function getHeadimgurlAttr($value, $data)
    {
        $wechat = $this->getWechat($data['wechat_id']);
        return $wechat['headimgurl'];
    }
    
    /**
     * 角色名称获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getRoleNameAttr($value, $data)
    {
        if ($data['role_id']){
            return MemberRoles::where('id', $data['role_id'])->value('name');
        }
        return  "暂无";
    }
    //获取角色图片
    public function getRoleIconUrlAttr($value,$data){
        if ($data['role_id']){
            return MemberRoles::where('id', $data['role_id'])->field('id,icon')->append(['icon_url'])->find()['icon_url'];
        }
        return  "";
    }
    /*
      * 角色名称获取器 20200323 新增
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     * */
    public function getRoleInfoAttr($value, $data)
	{
		return MemberRoles::field('commission_charge_type,commission_charge_first,commission_charge_second')->where('id', $data['role_id'])->find()->toArray();
	}
    /**
     * 获取微信用户信息
     * @param int $wechatId 微信用户 id
     */
    private function getWechat($wechatId)
    {
        return WechatUsers::field('nickname,headimgurl')->where('id', $wechatId)->find();
    }

	/**
	 * 获取分销商审核状态
	 * @param  string $value 字段值
     	 * @param  array  $data  所有数据
	 */
	public function getDistributionStatusAttr($value,$data)
	{
		$status = MembersDistribution::field('status')->where('userid', $data['id'])->find();//0未审核1审核通过2审核不通过3未申请
		return !empty($status) ? $status['status'] : 3;
	}
	//获取下级用户id
    public function getNextMembersIdAttr($value,$data){
	    $next_members = self::where("parent_id",$data['id'])->field('id')->select();
	    $next_members_id = [];
	    foreach ($next_members as $key => $value){
	        $next_members_id[] = $value['id'];
        }
	    return $next_members_id;
    }
    //获取商家
    public function getBusinessDataAttr($value,$data){
        return Business::where("member_id",$data['id'])->find();
    }
    //获取会员生日
    public function getMemberBirthdayAttr($value,$data){
	    return date('Y-m-d',$data['birthday']);
    }

}
